<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function trim(str) {
            let a = 0;
            while(str[a] == ' ') {
                a += 1;
            }
            let b = str.length - 1;
            while(str[b] == ' ') {
                b -= 1;
            }
            return str.slice(a, b + 1);
        }

        console.log(trim('  h l '))
        console.log(trim('   '))
    </script>
</body>
</html>